//
// Created by yunnan xu on 2021/1/15.
//

#include "PostProcessPass.h"
#include "EngineCore.h"
#include "Texture.h"


PostProcessPass<GraphicsAPI::GLESv3>::PostProcessPass()
{
    width = g_engine->width;
    height = g_engine->height;

    std::unordered_map<int, int> tex2d_params = {
            {GL_TEXTURE_MIN_FILTER, GL_LINEAR},
            {GL_TEXTURE_MAG_FILTER, GL_LINEAR},
            {GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE},
            {GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE},
    };

    framebufferTexture = std::make_shared<Texture2D<GraphicsAPI::GLESv3>> (GL_RGBA32I, 1, width, height, &tex2d_params, 0);

    GL_CHECK(glGenFramebuffers(1, &framebuffer));
    GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, framebuffer));
    GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER,
                                    GL_COLOR_ATTACHMENT0,
                                    GL_TEXTURE_2D,
                                    framebufferTexture->GetTexture(),
                                    0));

    GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0));
}

void PostProcessPass<GraphicsAPI::GLESv3>::Draw()
{
//    GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer));
//    GL_CHECK(glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST));
//    GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0));
}

void PostProcessPass<GraphicsAPI::GLESv3>::InitShaderUniformLocation()
{
//    GLint attrInxPos = glGetAttribLocation(program, "inPos" );
//    GLint locTexture = glGetUniformLocation(program, "u_texture" );
//    GLint locTexSize = glGetUniformLocation(program, "u_textureSize" );
//    GLint locSigma   = glGetUniformLocation(program, "u_sigma" );
//    GLint locDir     = glGetUniformLocation(program, "u_dir" );
}